﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DecisionTree
{
    class Image
    {
        public int Width { get; set; }
        public int Height { get; set; }

        private double[,] myImage;

        public Image(int width, int height)
        {
            myImage = new double[height, width];
            this.Width = width;
            this.Height = height;
        }

        /// <summary>
        ///   Sets given pixel given value
        /// </summary>
        /// 
        /// <param name="x">X value of coordinate, x is less than width.</param>
        /// <param name="y">Y value of coordiante, y is less than height.</param>
        /// <param name="value">Given double value, greater than 0.</param>
        /// 
        public void SetPixel(int x, int y, double value)
        {
            myImage[y, x] = value;
        }

        public void WriteImage(string path)
        {
            //var normalized = NormalizeMatrix(myImage);

            Bitmap bmp = new Bitmap(Width, Height);

            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    int t = Convert.ToInt32(myImage[j, i]);
                    bmp.SetPixel(i, j, Color.FromArgb(255, t, t, t));
                }
            }

            bmp.Save(path);
        }

        private double[,] NormalizeMatrix(double[,] myImage)
        {
            double maxElement = myImage.Cast<double>().Max();

            double[,] normalizedMatrix = new double[Width, Height];

            for (int i = 0; i < Height; i++)
                for (int j = 0; j < Width; j++)
                    normalizedMatrix[i, j] = (myImage[i, j] / maxElement) * 255;

            return normalizedMatrix;
        }
    }
}
